//index.js
const app = getApp()

Page({
  data: {
    envId: '',
    currentUser: '',
    userInfo: null,
    userPermission: null,
    permissionText: '',
    isAdmin: false,
    isSuperAdmin: false,
    powerList: [{
      title: '云函数',
      tip: '安全、免鉴权、免部署的云函数',
      showItem: false,
      item: [{
        title: '快速开始',
        page: 'quickstart'
      }, {
        title: '云函数教程',
        page: 'tcb'
      }]
    }, {
      title: '数据库',
      tip: '一个既可在小程序前端操作，也能在云函数中读写的数据库',
      showItem: false,
      item: [{
        title: '快速开始',
        page: 'database'
      }]
    }, {
      title: '文件存储',
      tip: '自带CDN加速文件存储',
      showItem: false,
      item: [{
        title: '快速开始',
        page: 'storage'
      }]
    }]
  },

  onLoad() {
    this.setData({
      envId: app.globalData.env || '未配置'
    });
    
    // 获取当前用户信息
    this.getCurrentUser();
  },

  // 获取当前用户信息
  getCurrentUser() {
    wx.showLoading({
      title: '加载中...'
    });

    wx.cloud.callFunction({
      name: 'quickstartFunctions',
      data: {
        type: 'getUserInfo'
      }
    }).then(result => {
      wx.hideLoading();
      
      if (result.result.success) {
        const { user, shop } = result.result.data;
        if (user) {
          const permission = user.permission || 3;
          const isAdmin = permission === 1 || permission === 2;
          
          this.setData({
            currentUser: user.username,
            userInfo: user,
            userPermission: permission,
            permissionText: this.getPermissionText(permission),
            isAdmin: isAdmin,
            isSuperAdmin: permission === 1
          });

          // 如果是管理员或超级管理员，跳转到管理员后台
          if (isAdmin) {
            wx.redirectTo({
              url: '/pages/admin/index'
            });
          }
        } else {
          // 用户不存在，跳转到用户资料页面
          wx.redirectTo({
            url: '/pages/userProfile/index'
          });
        }
      } else {
        wx.showToast({
          title: '获取用户信息失败',
          icon: 'error'
        });
      }
    }).catch(err => {
      wx.hideLoading();
      console.log('获取用户信息失败:', err);
      wx.showToast({
        title: '获取用户信息失败',
        icon: 'error'
      });
    });
  },

  // 获取权限文本
  getPermissionText(permission) {
    const permissionMap = {
      1: "超级管理员",
      2: "管理员", 
      3: "普通用户"
    };
    return permissionMap[permission] || "未知权限";
  },

  // 导航到用户信息测试页面
  navigateToTestUserInfo() {
    wx.navigateTo({
      url: '/pages/testUserInfo/index'
    });
  },

  // 导航到活动列表测试页面
  navigateToTestActivityList() {
    wx.navigateTo({
      url: '/pages/testActivityList/index'
    });
  },

  // 导航到排行榜页面
  navigateToLeaderboard() {
    wx.navigateTo({
      url: '/pages/leaderboard/index'
    });
  },

  navigateToTestPointsShop() {
    wx.navigateTo({
      url: '/pages/testPointsShop/index'
    });
  },

  navigateToTestBackpack() {
    wx.navigateTo({
      url: '/pages/testBackpack/index'
    });
  },

  // 跳转到饮品列表测试页面
  navigateToTestDrinksList() {
    wx.navigateTo({
      url: '/pages/testDrinksList/index'
    });
  },

  // 跳转到门票预约测试页面
  navigateToTestBooking() {
    wx.navigateTo({
      url: '/pages/testBooking/index'
    });
  },

  navigateToTestPoints() {
    wx.navigateTo({
      url: '/pages/testPoints/index'
    });
  },



  // 导航到签到测试页面
  navigateToTestSignIn() {
    wx.navigateTo({
      url: '/pages/testSignIn/index'
    });
  },

  // 导航到测试数据管理页面
  navigateToTestData() {
    wx.navigateTo({
      url: '/pages/testData/index'
    });
  },

  navigateToTestUserActions() {
    wx.navigateTo({
      url: '/pages/testUserActions/index'
    });
  },

  navigateToTestTicketPurchase() {
    wx.navigateTo({
      url: '/pages/testTicketPurchase/index'
    });
  },

  navigateToQRCodeGenerator() {
    wx.navigateTo({
      url: '/pages/qrCodeGenerator/index'
    });
  },

  navigateToTestRechargeSpecs() {
    wx.navigateTo({
      url: '/pages/testRechargeSpecs/index'
    });
  },


  // 导航到用户资料页面
  navigateToUserProfile() {
    wx.navigateTo({
      url: '/pages/userProfile/index'
    });
  },

  // 导航到用户资料页面
  navigateToUserProfile() {
    wx.navigateTo({
      url: '/pages/userProfile/index'
    });
  },

  // 导航到示例页面
  navigateToExample() {
    wx.navigateTo({
      url: '/pages/example/index'
    });
  },

  onClickPowerInfo(e) {
    const index = e.currentTarget.dataset.index;
    const powerList = this.data.powerList;
    powerList[index].showItem = !powerList[index].showItem;
    if (powerList[index].title === '云函数') {
      wx.showModal({
        title: '开发提示',
        content: '云函数需要部署后才能使用，请先在云开发控制台部署云函数',
        showCancel: false,
        confirmText: '我知道了'
      })
    }
    this.setData({
      powerList
    })
  },

  jumpPage(e) {
    const type = e.currentTarget.dataset.type;
    const page = e.currentTarget.dataset.page;
    if (type === 'cloud') {
      wx.navigateTo({
        url: `/pages/${page}/index`
      })
    } else if (type === 'default') {
      wx.navigateTo({
        url: `/pages/${page}/index`
      })
    }
  }
})
